package com.zx.mes.hyl.stack;

import com.zx.mes.hyl.array.HArray;

/**
 * @author huayunlong
 * @date 2018-7-12
 */
public class ArrayStack<E> implements Stack<E> {

    private int size;

    private HArray<E> array;


    public ArrayStack(int capacity){
        array = new HArray<>(capacity);
    }

    public ArrayStack(){
        array = new HArray<>();
    }


    public int getSize(){
        return size;
    }

    public int length(){
        return array.length();
    }

    public boolean isEmpty(){
        return size == 0;
    }




    @Override
    public void push(E e) {
        array.add(e);
        size++;
    }

    @Override
    public E pop() {
        E e = array.get(size - 1);
        array.removeLast();
        size--;
        return e;
    }

    @Override
    public E peek() {
        return array.get(size-1);
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("stack :[");
        for (int i = 0; i <array.length() ; i++) {
            sb.append(array.get(i));
            if (i < array.length() - 1) {
                sb.append(",");
           }
        }
        sb.append("]  " + array.length());
        return sb.toString();
    }
}
